package com.building18.service.impl;

import com.building18.handler.MyAssert;
import com.building18.mapper.RoleMapper;
import com.building18.mapper.UserMapper;
import com.building18.pojo.User;
import com.building18.pojo.dto.EditUserDTO;
import com.building18.pojo.dto.UserDTO;
import com.building18.pojo.enums.BuildingCode;
import com.building18.pojo.enums.Code;
import com.building18.pojo.vo.RoleVO;
import com.building18.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<User> selectUser() {
        return userMapper.selectUser();
    }

    @Override
    public Integer updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Override
    public Integer deleteUser(Integer id) {
        List<RoleVO> roleVOS = roleMapper.selectRoleById(id);
        MyAssert.error(!roleVOS.isEmpty(), "该用户存在角色，无法删除");
        return userMapper.deleteUser(id);
    }

    @Override
    public User login(UserDTO userDTO) {
        User user = userMapper.login(userDTO);
        MyAssert.error(user == null, BuildingCode.USERNAME_OR_PASSWORD_ERROR.getMessage());
        MyAssert.error(user.getStatus() == 0, BuildingCode.USER_STATUS_ERROR.getMessage());
        return user;
    }

    @Override
    public void beforeDeleteRole(Integer id) {
        List<Integer> userList=userMapper.beforeDeleteRole(id);
        MyAssert.error(!userList.isEmpty(), BuildingCode.ROLE_HAS_USER_RELATION.getMessage());
    }

    @Override
    public Integer updateUserData(EditUserDTO dto) {
        return userMapper.updateUserData(dto);
    }
}
